package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 分页条件查询
     */
    @Override
    public PageResponse<NursingProjectVo> queryNursingProject(String name, Integer pageNum, Integer pageSize, Integer status) {
        //分页查询
        PageHelper.startPage(pageNum, pageSize);
        //查询数据
        Page<NursingProject> pageInfo = nursingProjectMapper.queryNursingProject(name, status);
        // 封装护理项目关联护理计划数量(每一个护理项目关联几个护理计划)
        if (CollectionUtil.isEmpty(pageInfo.getResult())) {
            return PageResponse.of(pageInfo, NursingProjectVo.class);
        }
        // 得到每个护理项目id
        List<Long> ids = pageInfo.stream().map(item -> item.getId()).collect(Collectors.toList());
        //根据护理项目id查询每一个护理项目被引用的次数
        List<Map<String, Object>> maps = nursingProjectMapper.queryNursingPlanByids(ids);
        //  如果没有任何一个护理项目被引用, 直接返回
        if (CollectionUtil.isEmpty(maps)) {
            return PageResponse.of(pageInfo, NursingProjectVo.class);
        }
        //将护理项目引用数量集合转成Map集合 , 方便后期获取护理项目引用次数 Map<护理目id,关联数量>
        Map<Integer, Long> conutMap = maps.stream().collect(Collectors.toMap(map -> Integer.valueOf(map.get("projectId").toString()), map -> Long.valueOf(map.get("count").toString())));
        // 将实体类转化为VO
        List<NursingProjectVo> nursingProjectVoList = pageInfo.getResult().stream().map(item -> {
            NursingProjectVo bean = BeanUtil.toBean(item, NursingProjectVo.class);
            //获取护理项目对应的引用数量
            Long orDefault = conutMap.getOrDefault(item.getId().intValue(), 0L);
            bean.setCount(orDefault.intValue());
            return bean;
        }).collect(Collectors.toList());
        return PageResponse.<NursingProjectVo>builder()
                .page(pageInfo.getPageNum())
                .pageSize(pageInfo.getPageSize())
                .pages(Long.valueOf(pageInfo.getPages()))
                .total(pageInfo.getTotal())
                .records(nursingProjectVoList)
                .build();
    }

    /**
     * 根据id查询护理项目
     */
    @Override
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject =nursingProjectMapper.getById(id);
        return BeanUtil.toBean(nursingProject,NursingProjectVo.class);
    }

    /**
     * 修改护理项目详情
     */
    @Override
    public void modifyNurxsingProject(NursingProjectDto nursingProjectDto) {
        if(nursingProjectDto.getId() == null){
            throw new BaseException(BasicEnum.DATA_REFERENCED);
        }
        //如果护理项目被引用, 不能修改
        List<Map<String, Object>> maps = nursingProjectMapper.queryNursingPlanByids(List.of(nursingProjectDto.getId()));
        if (CollectionUtil.isNotEmpty(maps)){
            throw new BaseException(BasicEnum.PARAMETER_MISSING);
        }
        //修改数据库数据
        NursingProject bean = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.modifyNurxsingProject(bean);
    }

    /**
     * 新增护理项目
     */
    @Override
    public void addNursingProject(NursingProjectDto nursingProjectDto) {
        // 把请求类属性复制给实体类属性
        NursingProject bean = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.addNursingProject(bean);
    }

    /**
     *  启用禁用
     */
    @Override
    public void enableOrDisable(Long id, Integer status) {
        List<Map<String, Object>> maps = nursingProjectMapper.queryNursingPlanByids(List.of(id));
        // 判断maps有没有被引用,有被引用就不能禁用
        if(CollectionUtil.isNotEmpty(maps)){
            throw new BaseException(BasicEnum.DATA_REFERENCED);
        }
        nursingProjectMapper.enableOrDisable(id,status);
    }

    /**
     * 删除护理项目
     */
    @Override
    public void removeNursingProject(Long id) {
        List<Map<String, Object>> maps = nursingProjectMapper.queryNursingPlanByids(List.of(id));
        // 判断maps有没有被引用,有被引用就不能禁用
        if(!maps.isEmpty()){
            throw new BaseException(BasicEnum.DATA_REFERENCED);
        }
        nursingProjectMapper.removeNursingProject(id);
    }

    /**
     * 查询所有护理项目
     */
    @Override
    public List<NursingProjectVo> findNursingProjectList() {
        List<NursingProject> list= nursingProjectMapper.findNursingProjectList();
        return  BeanUtil.copyToList(list, NursingProjectVo.class);
    }
}
